/**
 * AlertAction.java
 * Created at 2018-1-5
 * Created by zhanghailin
 * Copyright (C) 2018 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.app.alert.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.broadtext.app.alert.service.AlertService;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.utils.DateUtils;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.system.service.ISysUserService;

/**
 * <p>ClassName: AlertAction</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2018-1-5</p>
 */
@Controller
@RequestMapping("/app/alert")
public class AlertAction {
    
    /**
     * 用户Service
     */
    @Autowired
    @Qualifier("sysUserService")
    private ISysUserService sysUserService;
    
    /**
     * 注入service
     */
    @Autowired
    @Qualifier("alertService")
    private AlertService alertService;
    
    
    /**
     * 
     * <p>Description: 告警列表查询</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findPackAlertDataList.mvc")
    @ResponseBody
    public List<Object> findPackAlertDataList(HttpServletRequest request, HttpServletResponse response) {
        List<Object> list = new ArrayList<Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        // 告警名称查询条件
        String alertNames = request.getParameter("alertName");
        if(StringUtils.isNotBlank(alertNames)){
            params.put("alertNames", alertNames.split(","));
        }
        // 告警级别查询条件
        String alertLevels = request.getParameter("alertLevel");
        if(StringUtils.isNotBlank(alertLevels)){
            params.put("alertLevels", alertLevels.split(","));
        }
        // 电池型号查询条件
        String packModels = request.getParameter("packModel");
        if(StringUtils.isNotBlank(packModels)){
            params.put("packModels", packModels.split(","));
        }
        // 电池型号查询条件
        String packCode = request.getParameter("packCode");
        if(StringUtils.isNotBlank(packCode)){
            params.put("packCode", packCode);
        }
        //获取请求用户名
        String loginName = request.getParameter("loginName");
        if(StringUtils.isNotBlank(loginName)) {
            try {
                Map<String, Object> checkMap = this.sysUserService.getUserClass(loginName);
                if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                    //该用户是客户账号
                    params.put("cusId", checkMap.get("cusId"));
                }
                list = this.alertService.findPackAlertDataList(params);
            } catch (DaoException e) {
                e.printStackTrace();
            }
            
        }
        return list;
    }
    
    
    /**
     * 
     * <p>Description: 告警列表查询</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findPackAlertHistoryData.mvc")
    @ResponseBody
    public List<Object> findPackAlertHistoryData(HttpServletRequest request, HttpServletResponse response) {
        List<Object> list = new ArrayList<Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        // 告警名称查询条件
        String alertNames = request.getParameter("alertName");
        if(StringUtils.isNotBlank(alertNames)){
            params.put("alertNames", alertNames.split(","));
        }
        // 告警级别查询条件
        String alertLevels = request.getParameter("alertLevel");
        if(StringUtils.isNotBlank(alertLevels)){
            params.put("alertLevels", alertLevels.split(","));
        }
        // 电池型号查询条件
        String packModels = request.getParameter("packModel");
        if(StringUtils.isNotBlank(packModels)){
            params.put("packModels", packModels.split(","));
        }
        // 电池型号查询条件
        String packCode = request.getParameter("packCode");
        if(StringUtils.isNotBlank(packCode)){
            params.put("packCode", packCode);
        }
        //获取请求用户名
        String loginName = request.getParameter("loginName");
        if(StringUtils.isNotBlank(loginName)) {
            try {
                Map<String, Object> checkMap = this.sysUserService.getUserClass(loginName);
                if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                    //该用户是客户账号
                    params.put("cusId", checkMap.get("cusId"));
                }
                list = this.alertService.findPackAlertHistoryData(params);
            } catch (DaoException e) {
                e.printStackTrace();
            }
            
        }
        return list;
    }
    
    /**
     * 
     * <p>Description: 根据电池id查询电池告警数据</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findAlertHistoryData.mvc")
    @ResponseBody
    public List<Object> findAlertHistoryData(HttpServletRequest request, HttpServletResponse response) {
        List<Object> list = new ArrayList<Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        //电池id
        String packCode = request.getParameter("packCode");
        params.put("packCode", packCode);
        //采集终端id
        String colClientId = request.getParameter("colClientId");
        params.put("colClientId", colClientId);
        list = this.alertService.findAlertHistoryData(params);
        return list;
    }
    
    
    /**
     * 
     * <p>Description: 查询告警详情</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findPackAlertDetailInfo.mvc")
    @ResponseBody
    public Map<String, Object> findPackAlertDetailInfo(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        //电池id
        String packCode = request.getParameter("packCode");
        params.put("packCode", packCode);
        //采集终端id
        String colClientId = request.getParameter("colClientId");
        params.put("colClientId", colClientId);
        //告警分类
        String alertCategory = request.getParameter("alertCategory");
        params.put("alertCategory", alertCategory);
        //告警时间
        String happenTime = request.getParameter("happenTime");
        params.put("happenTime", happenTime);
        data = this.alertService.findPackAlertDetailInfo(params);
        return data;
    }
    
    
    /**
     * 
     * <p>Description: 获取告警查询条件</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findAlertQueryParams.mvc")
    @ResponseBody
    public Map<String, Object> findAlertQueryParams(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> data = new HashMap<String, Object>();
        data = this.alertService.findAlertQueryParams();
        return data;  
    }
    
    /**
     * 
     * <p>Description: 获取告警查询条件</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getAlertCount.mvc")
    @ResponseBody
    public Map<String, Object> getAlertCount(HttpServletRequest request, HttpServletResponse response) {
       /* Map<String, Object> data = new HashMap<String, Object>();
        int number = new Random().nextInt(10) + 1;
        data.put("count", number);
        String beginTime = request.getParameter("beginTime");
        if(StringUtils.isNotBlank(beginTime)&&!"null".equals(beginTime)){
            data.put("count", 2);
        }else {
            data.put("count", 0);
        }
        return data;*/
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        String beginTime = request.getParameter("beginTime");
        if(StringUtils.isNotBlank(beginTime)&&!"null".equals(beginTime)){
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String dateStr = f.format(date);
            params.put("beginOfthisDay", dateStr);
            if(!beginTime.substring(8,10).equals(dateStr.substring(8,10))){
                beginTime = DateUtils.getDayStartStr(date);
                params.put("beginTime", beginTime);
            }else {
                params.put("beginTime", beginTime);
            }
            //获取请求用户名
            String loginName = request.getParameter("loginName");
            if(StringUtils.isNotBlank(loginName)) {
                try {
                    Map<String, Object> checkMap = this.sysUserService.getUserClass(loginName);
                    if(null != checkMap && "true".equals(checkMap.get("flag").toString())){
                        //该用户是客户账号
                        params.put("cusId", checkMap.get("cusId"));
                    }
                    Long count = this.alertService.getAlertCount(params);
                    data.put("count", count);
                } catch (DaoException e) {
                    e.printStackTrace();
                }
                
            }
        }else {
            data.put("count", 0);
        }
        return data;
    }

}
